package com.gxwz.document.business.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.gxwz.document.business.dao.IdocDao;
import com.gxwz.document.business.entity.Document;
import com.gxwz.document.business.service.IdocService;
import com.gxwz.document.business.util.SystemContext;
import com.gxwz.document.core.action.BaseAction;


@SuppressWarnings("serial")
public class DocAction extends BaseAction{
	protected static final String LIST_JSP = "/WEB-INF/page/literatureManage/Literature_list.jsp";
	protected static final String UPLOAD_JSP = "/WEB-INF/page/literatureManage/Literature_upload.jsp";
	
	private Document doc;
	private List<Document> ListDoc;
	private ServletContext context;
	private File[] file; 
	private String[] fileFileName;
	private InputStream fileInput;
	private int roleId;
	private String docpatch; //获取文件路径
	    //下载文件的文件名
     private String fileName; 
     private String fileType;//文件类型
     
     @Autowired
 	private IdocService docService;
 	@Autowired
 	private IdocDao docDAO;
 	
	/**
	 *文件上传
	
	 */
	public String uploaddoc() throws IOException{
		System.out.println("action");
		String targetDirectory = context.getRealPath(SystemContext.getTourProductFunctionUploadURL());  //文件的绝对路径
		try {
			if (file !=null && file.length>0) {
				
				for (int i = 0; i < file.length; i++) {
					String newName;
					String maxNumber = String.valueOf(docDAO.findMaxUserId());
					//System.out.println("maxNumber"+maxNumber);
					if("[null]".equals(maxNumber)){
						maxNumber = null;
						int a = 1001;
						String number = String.valueOf(a);
						newName= number;
					}
					else{
						//截取序号，并将其转换成int类型
						String numberString = maxNumber.substring(1, maxNumber.length()-1);
						int a = Integer.valueOf(numberString)+1;
						String number = String.valueOf(a);
						newName = number;
						
					}
					String upLoadFileName = newName+"-"+fileFileName[i];
					File destFile = new File(targetDirectory,upLoadFileName);
					File sourceFile = file[i];
					FileUtils.copyFile(sourceFile, destFile);
					String path = SystemContext.getTourProductFunctionUploadURL() + upLoadFileName;//保存文件的存放路径。target.getPath()为绝对路径，需要存相对路径
			       // serviceTaskFileService.addTaskFile(indexTaskService,upLoadFileName,path);//保存到文件表
				    FileInputStream in=new FileInputStream(destFile);
			        InputStreamReader is = new InputStreamReader(in,"utf-8");
			        if(destFile.exists()&&destFile.isFile()){
			        	destFile.length();//获取长度
			        	String url = path;  
			      	  //取得最后一个/的下标  
			      	  int index = url.lastIndexOf(".");  
			      	  
			      	  //将字符串转为字符数组  
			      	  char[] ch = url.toCharArray();  
			      	  //根据 copyValueOf(char[] data, int offset, int count) 取得最后一个字符串  
			      	  String lastString = String.copyValueOf(ch, index + 1, ch.length - index - 1);  
//			      	  System.out.println(lastString);  
			      	fileType=lastString;
			        }
			        else{
			        	System.out.println("这不是一个文件");
			        	
			        }
			       
			        double filesize=destFile.length()/1024/1024;
//			        String uid=UidUtils.UID();
			        docService.saveFileInfo(upLoadFileName, path,filesize,newName,fileType);//保存到文件表
//			        System.out.println("文件名"+upLoadFileName);
 
				}
				setForwardView(LIST_JSP);
				return SUCCESS;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			forwardView = LIST_JSP;
		}
		return null;	
			
	}

	public ServletContext getContext() {
		return context;
	}

	public void setContext(ServletContext context) {
		this.context = context;
	}

	public Document getDocument() {
		return doc;
	}

	public void setDocument(Document doc) {
		this.doc = doc;
	}

	public List<Document> getListDocument() {
		return ListDoc;
	}

	public void setListOaDocument(List<Document> listDoc) {
		ListDoc = listDoc;
	}

	public File[] getFile() {
		return file;
	}
	public void setFile(File[] file) {
		this.file = file;
	}
	public String[] getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String[] fileFileName) {
		this.fileFileName = fileFileName;
	}

	public InputStream getFileInput() {
		return fileInput;
	}
	public void setFileInput(InputStream fileInput) {
		this.fileInput = fileInput;
	}
	public String getFileName() {
		return fileName;
	}
	
	
	public String getDocpatch() {
		return docpatch;
	}
	public void setDocpatch(String docpatch) {
		 try {
			 docpatch = new String(docpatch.getBytes("ISO8859-1"),"UTF-8");
	        } catch (UnsupportedEncodingException e) {
	            throw new RuntimeException(e);
	        }	
		this.docpatch = docpatch;
	}
	
	
}
